package com.example.lottery.service.impl;

import com.example.lottery.common.errorcode.ServiceErrorCodeConstants;
import com.example.lottery.common.exception.ServiceException;
import com.example.lottery.controller.param.CreatePrizeParam;
import com.example.lottery.controller.param.FindPrizeListParam;
import com.example.lottery.controller.result.FindPrizeListResult;
import com.example.lottery.dao.dataobject.PrizeDO;
import com.example.lottery.dao.mapper.PrizeMapper;
import com.example.lottery.service.IPictureService;
import com.example.lottery.service.IPrizeService;
import com.example.lottery.service.dto.FindPrizeListDTO;
import com.example.lottery.service.dto.PrizeCreateDTO;
import com.example.lottery.service.dto.PrizeDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class PrizeServiceImpl implements IPrizeService {
    @Autowired
    private PrizeMapper prizeMapper;
    @Autowired
    private IPictureService pictureService;
    @Override
    public PrizeCreateDTO createPrize(CreatePrizeParam createPrizeParam, MultipartFile file) {
        //1-    上传图片存储到服务器磁盘中,以后可以通过文件名访问到图片
        String fileName = pictureService.savePicture(file);
        if(!StringUtils.hasLength(fileName)){
            log.warn("图片上传失败");
            throw new ServiceException(ServiceErrorCodeConstants.PICTURE_SAVE_ERROR);
        }
        //2-    将数据插入数据库中
        PrizeDO prizeDO=new PrizeDO();
        //将文件名作为索引存储在这个字段中 后期可通过127.0.0.1:8080/fileName访问到图片
        prizeDO.setImageUrl(fileName);
        prizeDO.setName(createPrizeParam.getPrizeName());
        prizeDO.setPrice(createPrizeParam.getPrice());
        prizeDO.setDescription(createPrizeParam.getDescription());
        int i = prizeMapper.insertSelective(prizeDO);
        if(i!=1){
            log.warn("插入数据失败");
            throw new ServiceException(ServiceErrorCodeConstants.PRIZE_INSERT_ERROR);
        }
        //3- 构造PrizeCreateDTO
        PrizeCreateDTO prizeCreateDTO=new PrizeCreateDTO();
        prizeCreateDTO.setId(prizeDO.getId());
        return prizeCreateDTO;
    }

    @Override
    public FindPrizeListDTO<PrizeDTO> findPrizeList(FindPrizeListParam findPrizeListParam) {
        //1-    参数校验
        if(!checkFindPrizeListParam(findPrizeListParam)){
            log.warn("奖品列表参数错误");
            throw new ServiceException(ServiceErrorCodeConstants.PRIZE_PARAM_ERROR);
        }
        //2-    根据currentPage当前页 PageSize 每页个数 构造sql查询语句
        // limit currentPage*SizePage,SizePage
        Integer param1=(findPrizeListParam.getCurrentPage()-1)* findPrizeListParam.getPageSize();
        Integer param2=findPrizeListParam.getPageSize();
        List<PrizeDO> records = prizeMapper.selectPrizeListByPage(param1,param2);

        //3-    查询奖品总数
        Integer total = prizeMapper.selectAllPrizeSize();

        //4-    将records映射给PrizeDTO
        List<PrizeDTO> prizeDTOS=records.stream()
                .map(
                        prizeDO -> {
                            PrizeDTO prizeDTO=new PrizeDTO();
                            prizeDTO.setId(prizeDO.getId());
                            prizeDTO.setName(prizeDO.getName());
                            prizeDTO.setDescription(prizeDO.getDescription());
                            prizeDTO.setImageUrl(prizeDO.getImageUrl());
                            prizeDTO.setPrice(prizeDO.getPrice());
                            prizeDTO.setGmtCreate(prizeDO.getGmtCreate());
                            prizeDTO.setGmtModified(prizeDO.getGmtModified());
                            return prizeDTO;
                        }
                ).collect(Collectors.toList());

        //5-    构造 FindPrizeListDTO<PrizeDTO>
        return new FindPrizeListDTO<PrizeDTO>(total,prizeDTOS);
    }

    private boolean checkFindPrizeListParam(FindPrizeListParam findPrizeListParam) {
        if(findPrizeListParam.getPageSize()<=0){
            return false;
        }
        if(findPrizeListParam.getCurrentPage()<=0){
            return false;
        }
        return true;
    }
}
